<?php

namespace app\api\controller\haibao;

use app\admin\model\HaibaoFavor;
use app\admin\model\HaibaoVideo;
use app\admin\model\Post;
use app\api\controller\Base;
use app\api\controller\Utils;
use think\Request;
use app\common\model\Category;

/**
 * @author zero
 * @Time 2018/12/27
 *
 * Class Videos
 * @package app\api\controller\haibao
 *
 * 嗨爆小片视频相关接口
 */
class Videos extends Base
{
    // 审核控制版本号
    public $control_version = '0.0.0';
    public $new_version = '2.0.0';
    public $version;
    public $util;
    public $user_id;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->util = new Utils();
        $this->version = $request->param('version') ?: '1.0.0';
        $this->user_id = $this->request->post('user_id') ?: -1;
    }

    // 获取单个分类下的视频
    public function get_videolist_by_categoryid($v, $id, $page = 1)
    {
        $list = HaibaoVideo::where('category_id', $id)->limit(($page - 1) * 5, 5)->order('id', 'desc')->select();
        // 格式化数据
        $category = Category::get($id);
        foreach ($list as $item) {
            $item->category_id = $category;
            $item->duration = $this->util->sec_to_time($item->duration);
        }
        $this->result('list', $list);
    }

    // 随机视频列表
    public function get_ramdom_list($v)
    {
        $list = $this->util->get_rand(5, 'haibao_video');
        $random_list = array();
        foreach ($list as $item) {
            $item['duration'] = $this->util->sec_to_time($item['duration']);
            $item['category_id'] = Category::get($item['category_id']);
            array_push($random_list, $item);
        }
        $this->result('random', array_values($random_list));
    }

    // 分类下的随机视频
    public function get_ramdomlist_by_categoryid($v, $id)
    {
        $videos = HaibaoVideo::where('category_id', $id)->select();
        $random_keys = array_rand($videos, 5);
        $category = Category::get($id);
        $list = array();
        foreach ($random_keys as $key) {
            $videos[$key]->duration = $this->util->sec_to_time($videos[$key]->duration);
            $videos[$key]->category_id = $category;
            array_push($list, $videos[$key]);
        }
        $this->result('list', $list);
    }

    // 获取单个视频详细信息
    public function get_video($v, $id)
    {
        $video = HaibaoVideo::get($id);
        if ($video) {
            $video->url = $this->get_tx_video_src($video->keyword);
            $video->vid = $video->keyword;
        }
        $this->result('video', $video);
    }

    /**
     * 2.0.0 版本接口
     */

    // 搜索建议词
    public function query_keyword()
    {
        $list = array('微信', '姑娘', '美味', '小姐姐', '有毒');
        $this->result('keyword', $list[array_rand($list, 1)]);
    }

    // 搜索视频
    public function query_video()
    {
        $keyword = $this->request->post('keyword');
        $page = $this->request->post('page') ?: 1;
        $list = HaibaoVideo::where('title', 'like', '%' . $keyword . '%')->limit(($page - 1) * 5, 5)->select();
        foreach ($list as $item) {
            $item->category_id = Category::get($item->category_id);
            $item->duration = $this->util->sec_to_time($item->duration);
        }
        $this->result('query', $list);
    }

    // 获取分类
    public function get_category()
    {
        $category_list = [
            array(
                'id' => 28,
                'name' => '体育',
            ),
            array(
                'id' => 23,
                'name' => '搞笑',
            ),
            array(
                'id' => 22,
                'name' => '美女',
            ),
            array(
                'id' => 29,
                'name' => '生活',
            ),
            array(
                'id' => 30,
                'name' => '科技',
            ),
        ];

        $this->result('category', $category_list);
    }

    // 获取单个分类下的视频
    public function get_videolist_by_category()
    {
        $id = $this->request->post('id');
        $page = $this->request->post('page') ?: 1;

        if ($this->version == $this->control_version) {
            $list = $this->get_post_list();
        } else {
            $list = HaibaoVideo::where('category_id', $id)->limit(($page - 1) * 5, 5)->order('id', 'desc')->select();
            // 格式化数据
            $category = Category::get($id);
            foreach ($list as $item) {
                $item->favor = HaibaoFavor::get(['account_id' => $this->user_id, 'favor_id' => $item->id]) ? true : false;
                $item->category_id = $category;
                $item->duration = $this->util->sec_to_time($item->duration);
            }
        }
        $this->result('list', $list);
    }

    // 推荐分类
    public function get_recommend_type()
    {
        $list = [
            array(
                'name' => '最新',
                'value' => 'new',
            ),
            array(
                'name' => '热点',
                'value' => 'hot',
            ),
            array(
                'name' => '收藏最多',
                'value' => 'collect',
            ),
            array(
                'name' => '分享最多',
                'value' => 'share',
            ),
        ];

        $this->result('recommend_type', $list);
    }

    // 获取单个推荐分类下的视频
    public function get_videolist_by_recommend_type()
    {
        $type = $this->request->post('type') ?: 'new';
        $page = $this->request->post('page') ?: 1;
        switch ($type) {
            case 'new':
                $field = 'updatetime';
                break;
            case 'hot':
                $field = 'play_num';
                break;
            case 'collect':
                $field = 'collect_num';
                break;
            case 'share':
                $field = 'share_num';
                break;
        }
        $list = HaibaoVideo::limit(($page - 1) * 5, 5)->order($field, 'desc')->select();
        // 格式化数据
        foreach ($list as $item) {
            $item->favor = HaibaoFavor::get(['account_id' => $this->user_id, 'favor_id' => $item->id]) ? true : false;
            $item->category_id = Category::get($item->category_id);
            $item->duration = $this->util->sec_to_time($item->duration);
        }
        $this->result('list', $list);
    }

    // 获取随机视频列表
    public function get_random_list()
    {
        $list = $this->util->get_rand(5, 'haibao_video');
        $random_list = array();
        foreach ($list as $item) {
            $item['duration'] = $this->util->sec_to_time($item['duration']);
            $item['category_id'] = Category::get($item['category_id']);
            array_push($random_list, $item);
        }
        $this->result('random', array_values($random_list));
    }

    // 分类下的随机视频
    public function get_randomlist_by_category()
    {
        $id = $this->request->post('id');
        if ($this->version == $this->control_version) {
            $random_list = $this->get_post_list();
        } else {
            $videos = HaibaoVideo::where('category_id', $id)->select();

            $random_keys = array_rand($videos, 5);
            $category = Category::get($id);

            $random_list = array();

            foreach ($random_keys as $key) {
                $videos[$key]->duration = $this->util->sec_to_time($videos[$key]->duration);
                $videos[$key]->category_id = $category;
                $videos[$key]->favor = HaibaoFavor::get(['account_id' => $this->user_id, 'favor_id' => $videos[$key]->id]) ? true : false;
                array_push($random_list, $videos[$key]);
            }
        }
        $this->result('random list', $random_list);
    }

    // 获取跳转其它小程序数据
    public function get_navigator_list()
    {
        $hezi = array(
            'name' => '百宝盒子',
            'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/suinigao/banner/bbhz.jpg',
            'target' => 'miniProgram',
            'url' => '',
            'appid' => 'wx3d3d7bd50ac38ff1',
        );
        $gcw = array(
            'name' => '天天广场舞教学',
            'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/suinigao/banner/ttgcwjx.jpg',
            'target' => 'miniProgram',
            'url' => '',
            'appid' => 'wx77a9968ac2c2d9e7',
        );
        $game = array(
            'name' => '蹦蹦Jump',
            'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/haibao/banner/bbjump.jpg',
            'target' => 'miniProgram',
            'url' => '',
            'appid' => 'wxfc2e85ad6e4c1691',
        );

        if ($this->version == $this->control_version) {
            $list = array($hezi, $gcw, $game);
        } else {
            $c = 0;
            $list = array();
            $ramdom_list = $this->util->get_rand(6, 'haibao_video');
            foreach ($ramdom_list as $item) {
                $data['name'] = $item['title'];
                $data['img'] = $item['cover'];
                $data['target'] = 'self';
                $data['url'] = '/pages/detail/detail?id=' . $item['id'];
                $data['appid'] = '';
                $c += 1;
                array_push($list, $data);
                if ($c == 2) {
                    array_push($list, $game);
                }
                if ($c == 4) {
                    array_push($list, $gcw);
                }
                if ($c == 6) {
                    array_push($list, $hezi);
                }
            }
        }

        $this->result('list', $list);
    }

    // 获取单个视频详细信息
    public function get_video_info()
    {
        $id = $this->request->post('id');
        if ($this->version == $this->control_version) {
            $item = Post::get($id);
        } else {
            $item = HaibaoVideo::get($id);
        }
        $this->result('info', $item);
    }

    // 获取腾讯视频播放地址
    public function get_tx_video_src($vid)
    {
        $url = 'http://vv.video.qq.com/getinfo';
        $params['vids'] = $vid;
        $params['platform'] = '101001';
        $params['charge'] = '0';
        $params['otype'] = 'json';

        $httpstr = $this->http($url, $params);
        $data = json_decode(substr($httpstr, 13, -1), true);

        $vi = $data['vl']['vi'][0];
        $fn = $vi['fn'];
        $fvkey = $vi['fvkey'];
        $data_url = $vi['ul']['ui'][0]['url'];

        $src = $data_url . $fn . '?vkey=' . $fvkey;

        return $src;
    }

    // 审核开启图文数据
    public function get_post_list()
    {
        return $this->util->get_rand(5, 'post');
    }

}
